/*******************************************************************************
 * Copyright (c) 2014-7-31 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.foreveross.project.projectonline.jfinal.plugin;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;

import com.foreveross.project.projectonline.jfinal.plugin.JedisSentinelKit.JedisMessage;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-7-31
 */
public class QueueKit implements Runnable {

	public static final String TYPE_NOTIFY = "notify";
	static String currentDate = "";//yyyyMMdd
	static QueueKit me = null;

	public static boolean publishNotifyToDate(Object message, Date date) {
		return JedisSentinelQueueProducer.create(
				"daily-" + TYPE_NOTIFY + "-"
						+ new SimpleDateFormat("yyyyMMdd").format(date))
				.publish(message);
	}

	public static void consume(final Method consume, final Object instance) {
		try {
			if (me == null) {
				synchronized (me) {
					if (me == null) {//double check
						QueueKit tmp = new QueueKit();
						new Thread(tmp).start();
						me = tmp;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		JedisSentinelQueueConsumer c = JedisSentinelQueueConsumer
				.create("daily-" + TYPE_NOTIFY + "-" + currentDate);
		c.consume(new JedisMessage() {
			public void onMessage(Object message) {
				try {
					consume.invoke(instance, new Object[] { message });
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void run() {
		while (true) {
			try {
				Date date = new Date();
				if (currentDate == null || currentDate.length() < 1) {
					currentDate = new SimpleDateFormat("yyyyMMdd").format(date);
				}
				Date tomorrow = DateUtils.addDays(date, 1);
				tomorrow = DateUtils.parseDate(new SimpleDateFormat("yyyyMMdd")
						.format(tomorrow)
						+ " 0301", "yyyyMMdd HHmm");
				long waitTime = tomorrow.getTime() - date.getTime();
				Thread.sleep(waitTime);
			} catch (Exception e) {
			}
		}
	}

	//	public static void main(String[] args) {
	//		//KryoRedisSerializer
	//		{
	//			Table table = new Table("TB_REPLY_CONTENT", TbReplyContent.class);
	//			try {
	//				//void setColumnTypeMap(Map<String, Class<?>> columnTypeMap)
	//				Method m = Table.class.getDeclaredMethod("setColumnTypeMap",
	//						Map.class);
	//				m.setAccessible(true);
	//				m.invoke(table,
	//						new Object[] { new HashMap<String, Class<?>>() });
	//			} catch (Exception e) {
	//				e.printStackTrace();
	//			}
	//			table.setColumnType("ID", String.class);
	//			table.setColumnType("REP_TITLE", String.class);
	//			TableMapping.me().putTable(table);
	//		}
	//		TbReplyContent reply = new TbReplyContent();
	//		{
	//			reply.set("ID", "id");
	//			reply.set("REP_TITLE", "回复标题");
	//		}
	//		System.out.println(reply.toJson());
	//		{
	//			KryoRedisSerializer serializer = new KryoRedisSerializer();
	//			byte[] bs = serializer.serialize(reply);
	//			TbReplyContent tmp = serializer.deserialize(bs);
	//			System.out.println(tmp.toJson());
	//		}
	//	}
}
